package hot100;

public class Solution82 {
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null) {
            return null;
        }
        ListNode newHead = new ListNode();
        ListNode last = newHead;
        last.next = head;
        ListNode prev = head;
        ListNode cur = head.next;
        while (cur != null) {
            int num = 0;
            while (cur != null && prev.val == cur.val) {
                num++;
                cur = cur.next;
            }
            if (num == 0) {
                last.next = prev;
                last = last.next;
                prev = prev.next;
            } else {
                last.next = cur;
                prev = cur;
            }
            cur = cur != null ? cur.next : null;
        }
        return newHead.next;
    }
}
